package labb2.del1;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

/**
 * Line.
 *
 * @author Mikko Puustinen
 * @version 0.7, 2007-sep-30
 */
public class Line extends Shape {
    private double x2;
    private double y2;

    /**
     * @param x
     * @param y
     * @param color
     * @param x2
     * @param y2
     */
    public Line(double x, double y, Color color, double x2, double y2) {
        super(x, y, color);
        this.x2 = x2;
        this.y2 = y2;
    }

    public double getX2() {
        return x2;
    }

    public double getY2() {
        return y2;
    }

    public void move() {
        super.move();
        x2 += getDX();
        y2 += getDY();
        super.constrain();
        constrain();
    }

    public void constrain() {
        Rectangle box = getBox();
        double x0 = box.x;
        double y0 = box.y;
        double x1 = x0 + box.width;
        double y1 = y0 + box.height;
        double dx = getDX();
        double dy = getDY();

        if (x2 <= x0) dx = Math.abs(dx);
        if (x2 >= x1) dx = -Math.abs(dx);
        if (y2 <= y0) dy = Math.abs(dy);
        if (y2 >= y1) dy = -Math.abs(dy);

        setVelocity(dx, dy);
    }

    /**
     * @param g Graphics.
     */
    public void paint(Graphics g) {
        g.setColor(getColor());
        g.drawLine((int) getX(), (int) getY(), (int) x2, (int) y2);
    }
}
